home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / Benchmark / Timer.php < prev   
Encoding:
PHP Script  |  2001-03-06  |  4.0 KB  |  170 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sebastian Bergmann <sb@phpOpenTracker.de>                   |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Timer.php,v 1.3 2000/09/29 08:29:22 sbergmann Exp $
  20. //
  21.  
  22.   /**
  23.   * Benchmark::Timer
  24.   * 
  25.   * Purpose:
  26.   * 
  27.   *   Timing Script Execution, Generating Profiling Information
  28.   * 
  29.   * Example:
  30.   * 
  31.   *   $timer = new Benchmark_Timer;
  32.   * 
  33.   *   $timer->start();
  34.   *   $timer->set_marker( "Marker 1" );
  35.   *   $timer->stop();
  36.   * 
  37.   *   $profiling = $timer->get_profiling();
  38.   * 
  39.   * @author   Sebastian Bergmann <sb@phpOpenTracker.de>
  40.   * @version  $Revision: 1.3 $
  41.   * @access   public
  42.   */
  43.  
  44.   class Benchmark_Timer
  45.   {
  46.     // {{{ properties
  47.  
  48.     /**
  49.     * Contains the markers
  50.     *
  51.     * @var    array
  52.     * @access public
  53.     */
  54.  
  55.     var $markers = array();
  56.     
  57.     // }}}
  58.     // {{{ start()
  59.  
  60.     /**
  61.     * Set "Start" marker.
  62.     *
  63.     * @see    set_marker(), stop()
  64.     * @access public
  65.     */
  66.  
  67.     function start()
  68.     {
  69.       $this->set_marker( "Start" );
  70.     }
  71.  
  72.     // }}}
  73.     // {{{ stop()
  74.  
  75.     /**
  76.     * Set "Stop" marker.
  77.     *
  78.     * @see    set_marker(), start()
  79.     * @access public
  80.     */
  81.  
  82.     function stop()
  83.     {
  84.       $this->set_marker( "Stop" );
  85.     }
  86.  
  87.     // }}}
  88.     // {{{ set_marker()
  89.  
  90.     /**
  91.     * Set marker.
  92.     *
  93.     * @param  string  name of the marker to be set
  94.     * @see    start(), stop()
  95.     * @access public
  96.     */
  97.  
  98.     function set_marker( $name )
  99.     {
  100.       $microtime = explode( " ", microtime() );
  101.       $this->markers[ $name ] = $microtime[ 1 ] . substr( $microtime[ 0 ], 1 );
  102.     }
  103.  
  104.     // }}}
  105.     // {{{ time_elapsed()
  106.  
  107.     /**
  108.     * Returns the time elapsed betweens two markers.
  109.     *
  110.     * @param  string  $start        start marker, defaults to "Start"
  111.     * @param  string  $end          end marker, defaults to "Stop"
  112.     * @return double  $time_elapsed time elapsed between $start and $end
  113.     * @access public
  114.     */
  115.  
  116.     function time_elapsed( $start = "Start", $end = "Stop" )
  117.     {
  118.       return bcsub( $this->markers[ $end ], $this->markers[ $start ], 6 );
  119.     }
  120.     
  121.     // }}}
  122.     // {{{ get_profiling()
  123.  
  124.     /**
  125.     * Returns profiling information.
  126.     *
  127.     * $profiling[ x ][ "name"  ] = name of marker x
  128.     * $profiling[ x ][ "time"  ] = time index of marker x
  129.     * $profiling[ x ][ "diff"  ] = execution time from marker x-1 to this marker x
  130.     * $profiling[ x ][ "total" ] = total execution time up to marker x
  131.     *
  132.     * @return array $profiling
  133.     * @access public
  134.     */
  135.  
  136.     function get_profiling()
  137.     {
  138.       $i = 0;
  139.       $total = 0;
  140.       $result = array();
  141.  
  142.       while( list( $marker, $time ) = each( $this->markers ) )
  143.       {
  144.         if( $marker == "Start" )
  145.         {
  146.           $diff = "-";
  147.         }
  148.  
  149.         else
  150.         {
  151.           $diff  = bcsub( $time,  $temp, 6 );
  152.           $total = bcadd( $total, $diff, 6 );
  153.         }
  154.  
  155.         $result[ $i ][ "name"  ] = $marker;
  156.         $result[ $i ][ "time"  ] = $time;
  157.         $result[ $i ][ "diff"  ] = $diff;
  158.         $result[ $i ][ "total" ] = $total;
  159.  
  160.         $temp = $time;
  161.         $i++;
  162.       }
  163.  
  164.       return $result;
  165.     }
  166.  
  167.     // }}}
  168.   }
  169. ?>
  170.